package classes.view.component
{
	import classes.controller.VONullObject;
	
	import com.d4p.custom.imageLoader.ImageLoader;
	import com.d4p.custom.imageLoader.ImageLoaderEvent;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class WaitingObject extends Sprite
	{
		
		private var _peopleStateIcon:WaitingPeople_icon;
		private var _name:TextField;
		private var _win:TextField;
		private var _lose:TextField;
		private var _ranking:TextField;
		private var _lv:MovieClip;
		private var _percent:TextField;
		private var _wintext:Sprite;
		private var _losetext:Sprite;
		private var _percentText_l:Sprite;
		private var _percentText_r:Sprite;
		private var _imgCont:Sprite;
		private var _emptyImage:Sprite;
		private var _black:Sprite;
		private var _objId:int;
		
		public function WaitingObject()
		{
			super();
			setLayOut();
			defaultSetting();
		}
		private function setLayOut():void
		{
			this._peopleStateIcon 	= this.getChildByName('state_icon')			as WaitingPeople_icon;
			this._name				= this.getChildByName('people_name')		as TextField;
			this._win				= this.getChildByName('win_txt')			as TextField;
			this._lose				= this.getChildByName('lose_txt')			as TextField;
			this._ranking			= this.getChildByName('ranking_txt')		as TextField;		
			this._lv				= this.getChildByName('lv_mc')				as MovieClip;
			this._percent			= this.getChildByName('percent_txt')		as TextField;
			this._wintext			= this.getChildByName('win_mc')				as Sprite;
			this._losetext			= this.getChildByName('lose_mc')			as Sprite;
			this._percentText_l		= this.getChildByName('percent_0')			as Sprite;
			this._percentText_r		= this.getChildByName('percent_1')			as Sprite;
			this._imgCont			= this.getChildByName('imgcont_mc')			as Sprite;
			this._emptyImage		= this.getChildByName('empty_mc')			as Sprite;
			this._black				= this.getChildByName('black_mc')			as Sprite;
			
		}
		
		public function resetObj():void
		{
			removeinit();
		}
		
		public function get objID():int
		{
			return _objId;
		}
		
		public function setWaitingObj($objId:int, $ready:int, $name:String, $level:String, $profileimage:String, $win:String, $lose:String, $ranking:String, $kingBool:int):void
		{
			_objId = $objId;
			
			_peopleStateIcon.num_people = $objId;
			if($kingBool == 1){
				//trace('방장');
				_peopleStateIcon.stateIcon = 0;
			}else{
				if($ready==0){
					//trace('ready');
					_peopleStateIcon.stateIcon = 1;
				}else{
					//trace('not ready');
					_peopleStateIcon.stateIcon = 2;
				}
			}

			this._name.text 		= VONullObject.getLoadInstance().nullText($name)+"님";
			this._name.width		= (_name.length)*15;
			
			this._lv.x				= Math.floor(this._name.x + (_name.length+2)*13)-8;
			this._lv.lv_txt.text	= VONullObject.getLoadInstance().nullText($level);
			var levelNum:int = Math.floor(Number($level)/10)+1;
			this._lv.gotoAndStop(levelNum);
			
			if(($profileimage == null) || ($profileimage == "undefined")){
				this.profileimageLoad	= "";
			}else{
				this.profileimageLoad	= $profileimage;
			}
			
			this._win.text 			= VONullObject.getLoadInstance().nullText($win);
			this._win.width			= _win.length * 12;
			this._wintext.x			= Math.floor(this._win.x + (_win.length * 6.2))+2;
			
			this._lose.x			= this._wintext.x + this._wintext.width+3;
			this._lose.text 		= VONullObject.getLoadInstance().nullText($lose);
			this._lose.width		= _lose.length * 12;
			this._losetext.x		= Math.floor(this._lose.x + (_lose.length * 6.2))+2;
			
			var loseNum:Number = Number($lose);
			var winNum:Number = Number($win);
			
			this._percentText_l.x	= this._losetext.x + this._losetext.width + 3;
			this._percent.text		= String(Math.round((winNum/(winNum+loseNum))*100))+" %";
			this._percent.x			= this._percentText_l.x + 5;
			this._percentText_r.x   = Math.round(_percent.text.length * 5.5) + this._percent.x;
			
			this._ranking.text 		= VONullObject.getLoadInstance().nullText($ranking);

			
			textSetting();
			
			init();
		}
		private function set profileimageLoad($url:String):void
		{
			if($url!=""){
				//imageload.
				var imgLoader:ImageLoader = new ImageLoader();
				imgLoader.load($url, false);
				imgLoader.addEventListener(ImageLoaderEvent.IMAGE_COMPLETE,	onCompleteImageLoad);	
			}else{
				this._emptyImage.visible = true;
			}
		}
		
		private function onCompleteImageLoad(e:ImageLoaderEvent):void
		{
			this._imgCont.addChild(e.bitmap);
			this._imgCont.alpha = 0;
			TweenLite.to(_imgCont, 0.3, {alpha:1});
		}
		
		private function defaultSetting():void
		{
			this._black.alpha = 0.5;
			this._lv.stop();
			this._emptyImage.visible = false;
		}
		private function init():void
		{
			this.addEventListener(MouseEvent.ROLL_OVER,	onRollOverHandler);
			
		}
		private function removeinit():void
		{
			this.removeEventListener(MouseEvent.ROLL_OVER,	onRollOverHandler);
			this.removeEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
		}
		private function onRollOverHandler(e:MouseEvent):void
		{
			TweenMax.to(_black, 0.4, {alpha:0.8, ease:Quart.easeInOut});
			this.addEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
		}
		private function onRollOutHandler(e:MouseEvent):void
		{
			TweenMax.to(_black, 0.2, {alpha:0.5, ease:Quart.easeInOut});
			this.removeEventListener(MouseEvent.ROLL_OUT,	onRollOutHandler);
		}
		
		private function textSetting():void
		{
			var nameformat_name:TextFormat			= new TextFormat();
			nameformat_name.letterSpacing			= -1;
			nameformat_name.color					= 0XEFAA3D;
			
			var nameformat_sir:TextFormat			= new TextFormat();
			nameformat_sir.letterSpacing			= -1;
			nameformat_sir.color					= 0XFFFFFF;
			
			var name_index:int = _name.text.indexOf('님');

			var textformat_letterspacing:TextFormat = new TextFormat();
			textformat_letterspacing.letterSpacing 	= -1;
			
			var number_letterspacing:TextFormat 	= new TextFormat();
			number_letterspacing.letterSpacing	 	= -1.5;
			
			this._name.setTextFormat(nameformat_name, 0, name_index);
			this._name.setTextFormat(nameformat_sir, name_index, name_index+1);
			
			this._win.setTextFormat(number_letterspacing);
			this._lose.setTextFormat(number_letterspacing);
			this._ranking.setTextFormat(number_letterspacing);
			this._percent.setTextFormat(number_letterspacing);
		}
	}
}